package com.nuumoblie.bluetoothtools.ble.db;


import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverter;
import androidx.room.TypeConverters;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.nuumoblie.bluetoothtools.AppApplication;
import com.nuumoblie.bluetoothtools.ble.db.converter.UUIDConverter;
import com.nuumoblie.bluetoothtools.ble.db.dao.BleCharacteristicDao;
import com.nuumoblie.bluetoothtools.ble.db.dao.BleDescriptorDao;
import com.nuumoblie.bluetoothtools.ble.db.dao.BleProductDao;
import com.nuumoblie.bluetoothtools.ble.db.dao.BleServiceDao;
import com.nuumoblie.bluetoothtools.ble.db.entity.BleCharacteristicEntity;
import com.nuumoblie.bluetoothtools.ble.db.entity.BleDescriptorEntity;
import com.nuumoblie.bluetoothtools.ble.db.entity.BleProductTypeEntity;
import com.nuumoblie.bluetoothtools.ble.db.entity.BleServiceEntity;

@Database(entities = {BleProductTypeEntity.class, BleServiceEntity.class,
        BleCharacteristicEntity.class, BleDescriptorEntity.class}, version = 1, exportSchema = false)
@TypeConverters(UUIDConverter.class)
public abstract class AdvertiseDatabase extends RoomDatabase {

    public static final String DB_NAME = "AdvertiseDatabase.db";
    private static volatile AdvertiseDatabase instance;

    public static synchronized AdvertiseDatabase getInstance(Context context) {
        if (instance == null) {
            instance = create(context);
        }
        return instance;
    }

    private static AdvertiseDatabase create(final Context context) {
        return Room.databaseBuilder(context, AdvertiseDatabase.class, DB_NAME)
                .allowMainThreadQueries()
                .addCallback(new DBCallback())
                .build();
    }

    public abstract BleProductDao getProductDao();

    public abstract BleServiceDao getServiceDao();

    public abstract BleCharacteristicDao getCharacteristicDao();

    public abstract BleDescriptorDao getDescriptorDao();

    private static class DBCallback extends Callback{
        @Override
        public void onCreate(@NonNull SupportSQLiteDatabase db) {
            super.onCreate(db);
//            InitDatabase.initDatabase(AppApplication.getInstance());
        }
    }

}
